package com.example.library_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.library_demo.dto.book.CheckInBookDTO;
import com.example.library_demo.entity.Book;
import com.example.library_demo.entity.Shelf;
import com.example.library_demo.exception.BizException;
import com.example.library_demo.mapper.BookMapper;
import com.example.library_demo.service.IBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.library_demo.service.IShelfService;
import com.example.library_demo.vo.book.BookInfoVO;
import com.example.library_demo.vo.book.BookVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Eric
 * @since 2024-11-27
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {

    @Autowired
    private IShelfService shelfService;

    @Override
    public IPage<BookVO> pageBookVO(int pageNum, int pageSize) {
        return baseMapper.selectPageAll(new Page(pageNum,pageSize));
    }

    @Override
    public IPage<Book> find(String text,int pageNum, int pageSize) {
        LambdaQueryWrapper<Book> wrapper = Wrappers.lambdaQuery();
        wrapper.like(Book::getName,text).or().like(Book::getAuth,text);
        return baseMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
    }

    @Override
    public List<BookInfoVO> findBookByShelfId(long shelfId) {
        return baseMapper.selectBooksByShelfId(shelfId);
    }

    @Override
    public IPage<BookVO> listStorageBooks(int pageNum, int pageSize) {
        return baseMapper.selectStorageBook(new Page(pageNum,pageSize));
    }


    @Override
    public Boolean checkIn(CheckInBookDTO dto) {
        Shelf shelf = shelfService.getById(dto.getShelfId());
        if(null==shelf){
            throw new BizException("Shelf is not exist");
        }
        LambdaQueryWrapper<Book> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Book::getBookId,dto.getBookId())
                .eq(Book::getStatus,0);//already check in, can not check in again
        Book book = getOne(wrapper);
        if(book == null){
            throw new BizException("Book is not exist");
        }
        book.setShelfId(dto.getShelfId());
        book.setCount(dto.getCount());
        book.setPrice(dto.getPrice());
        book.setScore(dto.getScore());
        book.setStatus(1);
        return updateById(book);
    }

    @Override
    public Boolean checkOut(Long bookId) {
        Book book = getById(bookId);
        if(null==book){
            throw new BizException("Book is not exist");
        }
        book.setShelfId(null);
        book.setStatus(0);
        return updateById(book);
    }
}
